// pages/admin/adminjoindata/adminjoindata.js
import sendRequest from '../../rpromise/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        getyear:[],
        getterm:['第一学期','第二学期'],
        sdata:{},
        year:2022,
        term:'第一学期',
        activity:[],
        selecterm:[],
        sum_count:0,
        getstype:['校级','院级'],
        stype:'校级',
        right: 10,
        top: 800,
        width: 120,
        height: 120,
        startX: 0,
        startY: 0,
    },
    selectYearBtn(e){
        this.setData({
            year:this.data.getyear[e.detail.value]
        })
        console.log(this.data.year)
        this.showCount(this.data.year,this.data.term,this.data.stype)
    },
    selectTermBtn(e){
        this.setData({
            term:this.data.getterm[e.detail.value]
        })
        console.log(this.data.term)
        this.showCount(this.data.year,this.data.term,this.data.stype)
    },
    async selectStypeBtn(e){
        this.setData({
            stype:this.data.getstype[e.detail.value]
        })
        console.log(this.data.stype)
        // this.loadData()
        this.showCount(this.data.year,this.data.term,this.data.stype)
    },
    getTime(){
        //20200108070702
        var result = '2020'
        var yy = new Date().getFullYear()
        var years = []
        for (let index = result; index < yy; index++) {
            years.push(index+'—'+(index+1))
        }
        this.setData({
            year:result+'—'+(result+1),
            getyear:years
        })
        console.log(this.data.year)
    },
    loadData(){
        var sql = "SELECT atime,stype,sa.sno,sname,sd,sgrade,smajor,SUM(count) as count FROM sa,student where sa.sno=student.sno and incheck='Y' and outcheck='Y' GROUP BY sa.sno,stype" 
        this.sqlRequest(sql).then(res=>{
            console.log(res.data)
            this.setData({
                activity:res.data
            })
            // this.showCount(this.data.year,this.data.term)
        })
        
    },
    showCount(year,term,stype){
        var years = year.split("—")
        var last_term = years[0]+"-06-30"
        var next_term = years[1]+"-01-01"
        var next_term2 = years[1]+"-06-30"
        var adata = []
        if(term==='第一学期'){
            this.data.activity.forEach(item => {
                if(item.atime<=next_term&&item.atime>=last_term&&item.stype===stype){
                    adata.push(item)
                }
            });
            this.setData({
                selecterm:adata,
            })
        }else{
            this.data.activity.forEach(item => {
                if(item.atime>next_term&&item.atime<=next_term2&&item.stype===stype){
                    adata.push(item)
                }
            });
            this.setData({
                selecterm:adata,
            })
        }
        console.log(this.data.selecterm)

    },
    downLoadExcel() {
        var year = this.data.year
        var term = this.data.term
        var stype = this.data.stype

        var years = year.split("—")
        var last_term = years[0]+"-06-30"
        var next_term = years[1]+"-01-01"
        var next_term2 = years[1]+"-06-30"

        if(term==='第一学期'){
            var sql="SELECT sa.sno,sname,sd,sgrade,smajor,stype,SUM(count) as count FROM sa,student where sa.sno=student.sno and incheck='Y' and outcheck='Y' and atime<='"+next_term+"' and atime>='"+last_term+"' and stype='"+stype+"' GROUP BY sa.sno,stype"
        }else{
            var sql="SELECT sa.sno,sname,sd,sgrade,smajor,stype,SUM(count) as count FROM sa,student where sa.sno=student.sno and incheck='Y' and outcheck='Y' and atime>'"+next_term+"' and atime<='"+next_term2+"' and stype='"+stype+"' GROUP BY sa.sno,stype"
        }
        // console.log(this.data.selecterm)
        // "SELECT sa.sno,sname,stype, SUM(count) as count FROM sa,student where sa.sno=student.sno GROUP BY sa.sno,stype"
        //通过wx.openDocument 打开文档，openDocument 提供了一个属性showMenu 显示右上角的菜单
        
        wx.downloadFile({
            url: 'http://43.139.234.245:8900/excel?sql='+sql,
            success(res) {
                if (res.statusCode === 200) {
                    const filePath = res.tempFilePath;
                    wx.openDocument({
                        filePath: filePath,
                        showMenu: true,
                        success: function (res) {
                            console.log(res, '打开文档成功');
                        }
                    });
                }
            }
        })
    },
    onTouchStart: function (e) {
        this.setData({
            startX: e.touches[0].clientX,
            startY: e.touches[0].clientY
        })
    },
    onTouchMove: function (e) {
        var offsetX = e.touches[0].clientX - this.data.startX
        var offsetY = e.touches[0].clientY - this.data.startY
        this.setData({
            right: this.data.right - offsetX,
            top: this.data.top + offsetY,
            startX: e.touches[0].clientX,
            startY: e.touches[0].clientY
        })
    },
    onTouchEnd: function (e) {
        this.setData({
            right: 10
        })

    },
    async sqlRequest(sql) {
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql
            },
        })
        // console.log(res1)
        return res1
    },
   
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            sdata:wx.getStorageSync('student')
        })
        this.getTime()
        this.loadData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        wx.setNavigationBarTitle({
            title: '活动统计',
          })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})